package com.future.lock.home.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.view.CustomViewCenterDialog;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.home.adapter.ChooseGatewaysAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddLockStep1Activity extends BaseActivity {

    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.tv_next)
    TextView tvNext;
    private boolean hasGateway;
    private CustomViewCenterDialog gatewayChooseDialog;
    private ChooseGatewaysAdapter adapter;
    private String gatewayMac = "";
    private String gateway_id;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_lock_step1);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("添加智能锁");
        if (!hasGateway){
            imageView.setImageResource(R.mipmap.ic_gateway_gray);
            textView.setText("您还没有添加网关，请先添加网关后再添加智能锁");
            tvNext.setText("添加网关");
        }
    }

    private void chooseGateway() {
        View rootView = getLayoutInflater().inflate(R.layout.dialog_gateway_choose, null);
        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ChooseGatewaysAdapter(R.layout.item_choose_gateway, Constants.gatewayList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            ((ChooseGatewaysAdapter) adapter).setChoosePosition(position);
            adapter.notifyDataSetChanged();
        });
        rootView.findViewById(R.id.btn_ok).setOnClickListener(view -> {
            gatewayMac = adapter.getData().get(adapter.getChoosePosition()).gatewayId;
            gatewayChooseDialog.dismiss();
            Bundle bundle = new Bundle();
            bundle.putString("mac",gatewayMac);
            startActivity(AddLockStep2Activity.class,bundle);
        });
        gatewayChooseDialog = new CustomViewCenterDialog(this)
                .setView(rootView)
                .builder()
                .setCancelable(false)
                .show();
    }

    @Override
    protected void initLogic() {
        hasGateway = Constants.gatewayList!=null && Constants.gatewayList.size()>0;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        gateway_id = extras.getString("gateway_id");
    }


    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        if (hasGateway) {
            if (!TextUtils.isEmpty(gateway_id)) {
                Bundle bundle = new Bundle();
                bundle.putString("mac",gateway_id);
                startActivity(AddLockStep2Activity.class,bundle);
            }else {
                chooseGateway();
            }
        }else {
            startActivity(AddGatewayStep1Activity.class);
        }
    }
}
